package com.dao;

import com.bean.LoginInfo;
import com.utils.JdbcUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserDao {

    //todo 为了简单起见本人并无配置mybatis 直接用的dbutil工具类同上一个javaweb
    public boolean checkUser(String username, String password) {
        List<List<Object>> lists = JdbcUtil.selectSqlToList("select * from user where name=?", username);
        boolean ok = false;
        //检查是否存在用户名称和密码
        for (List<Object> list : lists) {
            if (list.get(2).equals(password) && list.get(1).equals(username)) {
                ok = true;
                break;
            }
        }

        return ok;
    }

    public void insertLoginInfo(LoginInfo loginInfo) {

        JdbcUtil.insertOrUpdate("insert into login_info values(?,?,?)", loginInfo.getName(),
                loginInfo.getTime(), loginInfo.getIp());

    }

    public List<LoginInfo> getLoginLog(String name) {
        List<List<Object>> lists = JdbcUtil.selectSqlToList("select * from login_info where name=?", name);
        List<LoginInfo> res = new ArrayList<>();
        for (List<Object> list : lists) {
            res.add(new LoginInfo((String) list.get(0), (String) list.get(1), (String) list.get(2)));
        }

        return res;
    }
}
